import 'package:freezed_annotation/freezed_annotation.dart';

part 'response_data.freezed.dart';
part 'response_data.g.dart';

/// 响应数据实体
@Freezed(genericArgumentFactories: true)
class ResponseData<T> with _$ResponseData<T> {
  factory ResponseData({
    @DataConverter() T? data,
    @Default(-1) int code,
    @Default('') String message,
  }) = _ResponseData<T>;

  factory ResponseData.fromJson(
          Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
      _$ResponseDataFromJson(json, fromJsonT);
}

class DataConverter<T> implements JsonConverter<T, Object?> {
  const DataConverter();

  @override
  T fromJson(Object? json) {
    return json as T;
  }

  @override
  Object? toJson(T object) {
    return object;
  }
}
